package com.estar.assistsurvy.utils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import com.estar.assistsurvy.constant.Constants;
import android.util.Log;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpUtils {
	
	private OkHttpClient client;
//	private SharedPreferences userPrefer,rolePrefer;
//	private Activity activity;
//	private Intent intent;

	public void postRequest(String json){
//		String url_str="http://124.172.245.188:5859/estargps/mobile";
//		String xml="<?xml version=\"1.0\" encoding=\"GB2312\"?><root><gpsInfo deviceID=\"\" sim=\"P9999wuzc\" simSign=\"P10376106009\" longitude=\""+longitude+"\" latitude=\""+latitude+"\" type=\"1\"/><head method=\"qGPSInfo\" function=\"qPDAGPSInfo\"/><cell_towers mobile_network_code=\"00\" cell_id=\"1751\" mobile_country_code=\"460\" isp=\"\" location_area_code=\"42332\" getInfo=\"Y\"/></root>";
		
		String url_str=Constants.NAMESPACE+Constants.UPLOAD_LOCATION;
		client=new OkHttpClient.Builder()
				.connectTimeout(5, TimeUnit.SECONDS)
				.readTimeout(5, TimeUnit.SECONDS)
				.build();
		
		RequestBody body=RequestBody.create(Constants.JSON, json);
		Request request=new Request.Builder()
				.url(url_str)
				.post(body)
				.build();

		try {
			Response response = client.newCall(request).execute();
			if(response.isSuccessful()){
//				Log.i("wl", "上传成功");
//				Log.i("wl", "上传GPS返回值"+response.body().string());
//				ResponseMsg location=new ResponseMsg();
//				Gson gson=new Gson();
//				location=gson.fromJson(response.body().string(),ResponseMsg.class);
//				activity=ActivityController.getCurrentActivity();
//				if(location.getCode().equals("0")){
//					new AlertDialog.Builder(activity, AlertDialog.THEME_HOLO_LIGHT)
//					.setMessage(location.getMessages())
//					.create().show();
//				}else if(location.getCode().equals("1")){
//					
//				}else if(location.getCode().equals("2")){
//					new AlertDialog.Builder(activity, AlertDialog.THEME_HOLO_LIGHT)
//					.setMessage(location.getMessages())
//					.setNegativeButton("退出", new OnClickListener() {
//						
//						@Override
//						public void onClick(DialogInterface dialog, int which) {
//							// TODO Auto-generated method stub
//							userPrefer.edit().clear().commit();
//							rolePrefer.edit().clear().commit();
////							JPushInterface.setAliasAndTags(getActivity().getApplicationContext(), "", null,null);
////							ExitUtils.ExitApp(getActivity());
//							JPushInterface.setAliasAndTags(activity.getApplicationContext(), "", null,null);
//							intent=new Intent(activity, LoginActivity.class);
//							activity.startActivity(intent);
//							activity.finish();
//						}
//					})
//					.setPositiveButton("重新登录", new OnClickListener() {
//						
//						@Override
//						public void onClick(DialogInterface dialog, int which) {
//							// TODO Auto-generated method stub
//							userPrefer.edit().clear().commit();
//							rolePrefer.edit().clear().commit();
//							JPushInterface.setAliasAndTags(activity.getApplicationContext(), "", null,null);
//							intent=new Intent(activity, LoginActivity.class);
//							activity.startActivity(intent);
//							activity.finish();
//						}
//					})
//					.create()
//					.show();
//				}
			}
			response.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
